<?PHP
include("inc/config.php");

$_TABLE = $_CONF->get("CONFIG>TABLES>TABLE.name=".$_GET['table'].".node");
$tableTitle = $_CONF->get("TABLE.title", $_TABLE);
$tableFields = $_DB->getFields($_GET['table']);

$_TABLE2 = new table($_GET['table']);

$_FIELD = $_CONF->get("TABLE>FIELDS>FIELD.name=".$_GET['field'].".node", $_TABLE);
$fieldTitle = $_CONF->get("FIELD.title", $_FIELD);
$fieldType = $tableFields[$_GET['field']];
$fieldValue = $_DB->query("SELECT ".$_GET['field']." FROM ".$_GET['table']." WHERE ".$_GET['idField']." = '".$_GET['id']."'");
$data = $fieldValue[0][0];

if($_FIELD != false) {
	if($_CONF->get("FIELD.multiple", $_FIELD) == "true") $fieldType = "multiple";
	else if($_CONF->get("FIELD.type", $_FIELD) == "filesystem") $fieldType = "filesystem";
	else if($_CONF->get("FIELD>VIRTUALCROSS.node", $_FIELD) != false) $fieldType = "virtualcross";
	else if($_CONF->get("FIELD>CROSS.node", $_FIELD) != false) $fieldType = "cross";
	else $fieldType = "normal";
}

?>
<html>
<head>
	<title>Config crossfields</title>
	<style type="text/css">@import 'css/common.css';</style>
	<script type="text/javascript" src="js/common.js"></script>
</head>
<body>

	<div id="popupContent">
		<form id="fastEditForm" method="post" enctype="multipart/form-data">
		<input type="hidden" name="field" value="<?=$_GET['field']?>">
		<input type="hidden" name="table" value="<?=$_GET['table']?>">
		<input type="hidden" name="idField" value="<?=$_GET['idField']?>">
		<input type="hidden" name="idValue" value="<?=$_GET['id']?>">
		<table>
		<tr valign="top"><td><h2>Fast editor</h2></td></tr>
		<tr><td colspan="4"><p>Tabella: <strong><?=(($tableTitle)? $tableTitle : $_GET['table'])?></strong> | Campo: <strong><?=(($fieldTitle) ? $fieldTitle : $_GET['field']);?></strong></p></td></tr>
		<tr><td colspan="4"><br>
		<?PHP
		
		switch($fieldType) {
			case "multiple":
				$data = $fieldValue[0][0];
				$crossTable = $_CONF->get("FIELD>CROSS.table", $_FIELD);
				$ref = $_CONF->get("FIELD>CROSS.ref", $_FIELD);
				$cross = $_DB->query("select * from " . $crossTable);
				$title = $_CONF->get("FIELD>CROSS.title", $_FIELD);
				$fieldName = $_GET['field'];
				?>
				<table>
				<tr><td>
				<strong>Valori disponibili</strong><br>
				<select id="<?=$_GET['field']?>_multivalue" multiple style="height:200px; width:180px ">
				<?
				$sep = $_CONF->get("CONFIG>ADMIN>MULTIFIELDSEPARATOR.nodeValue");
				$multiValues = explode($sep, $data);
				$fieldName = $_GET['field'];
				foreach($cross as $row => $value) {
					if(!in_array($value[$ref], $multiValues)) echo '<option value="'.$value[$ref].'">' . $value[$title] . '</option>';
				}
				?>
				</select>
				</td><td>
				<a href="javascript:FF.contents.moveOptions($('<?=$fieldName?>_multivalue'), $('<?=$fieldName?>_target'), $('<?=$fieldName?>_target'), $('<?=$fieldName?>'))">>></a><br>
				<br>
				<a href="javascript:FF.contents.moveOptions($('<?=$fieldName?>_target'), $('<?=$fieldName?>_multivalue'), $('<?=$fieldName?>_target'), $('<?=$fieldName?>'))"><<</a>
				</td><td>
				<strong>Valori selezionati</strong><br>
				<select name="<?=$fieldName?>_target" id="<?=$fieldName?>_target" multiple style="height:200px; width:180px ">
				<?
				
				foreach($cross as $row => $value) {
					if(in_array($value[$ref], $multiValues)) echo '<option value="'.$value[$ref].'">' . $value[$title] . '</option>';
				}
				?>
				</select>
				<input type="hidden" id="<?=$fieldName?>" name="<?=$fieldName?>" value="<?=$data?>">
				</td></tr>
				</table>
				<?
				break;
				
				
			case "filesystem":
				// Generalmente immagine o comunque campo su filesystem
				$loadPath = $_CONF->get("CONFIG>ADMIN>ADMINLOADINGPATH.nodeValue");
				$imgPath = $loadPath.$_GET['table']."/".$fieldValue[0][0];
				?>
				<h1>BETA</h1>
				<table>
				<tr valign=top><td>
				<?
				if(is_file($_SERVER['DOCUMENT_ROOT'].$imgPath)) echo '<a href="'.$imgPath.'" target="_NEW" title="Vedi immagine"><img id="previewer_'.$_GET['field'].'" src="'.$imgPath.'" width="150" /></a>';
				else echo '<img id="previewer_'.$_GET['field'].'" src="i/imageNotFound.gif" width="150" />';
				?>
				</td>
				<td>Nome del file: <strong id="filename_<?=$_GET['field']?>"><?=$imgPath?></strong> | &nbsp;<input type="button" id="btnRemove_<?=$name?>" onClick="FF.contents.removeUploadedFile('<?=$imgPath?>', '<?=$_GET['table']?>', '<?=$name?>');" value="CANCELLA"><br><br>
				Seleziona da directory:<br>
					<input type="hidden" name="<?=$_GET['field']?>" value="$fieldValue[0][0]">
					<input type="hidden" name="loadingType_<?=$_GET['field']?>" value="LOAD" />
					<input type="hidden" name="remove_<?=$_GET['field']?>" id="remove_<?=$_GET['field']?>" value="" />
					<select id="fileSelect_<?=$_GET['field']?>" name="select_<?=$_GET['field']?>" onChange="FF.contents.selectImage('<?=$_GET['field']?>',  '<?=$loadPath.$_GET['table']."/"?>' + this.options[this.selectedIndex].value, 'SELECT', $('fastEditForm'));">
					<option value=""> -- </option>
					<?=$_TABLE2->getUploadDirFiles($fieldValue[0][0])?>
					</select>
				<br /><br />
				Seleziona da disco:<br />
				<input type="file" name="<?=$_GET['field']?>_filesystem" value="none" onChange="FF.contents.selectImage('<?=$_GET['field']?>', this.value, 'BROWSE', $('fastEditForm'));" >
				</td></tr>
				</table>
				<?
				break;
				
				
			case "cross":
				$fieldName = $_GET['field'];
				$tabella = $_CONF->get("FIELD>CROSS.table", $_FIELD);
				$ref = $_CONF->get("FIELD>CROSS.ref", $_FIELD);
				$title = $_CONF->get("FIELD>CROSS.title", $_FIELD);
				
				$cross = $_DB->query("select * from $tabella");
				$multiple = $_CONF->get("FIELD.multiple", $_FIELD);
				if($multiple == "true") { ?>
				
					<table>
					<tr><td>
					<strong>Valori disponibili</strong><br>
					<select id="<?=$_CONF->get("FIELD.name", $_FIELD);?>_multivalue" multiple style="height:200px ">
					<?
					$sep = $_CONF->get("CONFIG>ADMIN>MULTIFIELDSEPARATOR.nodeValue");
					$multiValues = explode($sep, $data);
					$fieldName = $name;
					foreach($cross as $row => $value) {
						if(!in_array($value[$ref], $multiValues)) echo '<option value="'.$value[$ref].'">' . $value[$title] . '</option>';
					}
					?>
					</select>
					</td><td>
					<a href="javascript:FF.contents.moveOptions($('<?=$fieldName?>_multivalue'), $('<?=$fieldName?>_target'), $('<?=$fieldName?>_target'), $('<?=$fieldName?>'))">>></a><br>
					<br>
					<a href="javascript:FF.contents.moveOptions($('<?=$fieldName?>_target'), $('<?=$fieldName?>_multivalue'), $('<?=$fieldName?>_target'), $('<?=$fieldName?>'))"><<</a>
					</td><td>
					<strong>Valori selezionati</strong><br>
					<select name="<?=$fieldName?>_target" id="<?=$fieldName?>_target" multiple style="height:200px ">
					<?
					
					foreach($cross as $row => $value) {
						if(in_array($value[$ref], $multiValues)) echo '<option value="'.$value[$ref].'">' . $value[$title] . '</option>';
					}
					?>
					</select>
					<input type="hidden" id="<?=$fieldName?>" name="<?=$fieldName?>" value="<?=$data?>">
					</td></tr>
					</table>
					
				<? } else {
					?><select name="<?=$fieldName?>"><?
					foreach($cross as $row => $value) {
						echo '<option value="'.$value[$ref].'" '.(($data == $value[$ref])?"selected":"").'>' . $value[$title] . '</option>';
					}
					?></select><?
				}
				break;
				
			case "virtualcross":
				$fieldName = $_GET['field'];
				echo '<select name="'.$_GET['field'].'">';
				$virtualCross = $_CONF->get("FIELD>VIRTUALCROSS.childNodes", $_FIELD);
				foreach($virtualCross as $row => $value) {
					echo '<option value="'.$_CONF->get("DATA.value", $value).'" '.(($data == $_CONF->get("DATA.value", $value))?"selected":"").'>' . $_CONF->get("DATA.label", $value) . '</option>';
				}
				echo '</select>';
				break;
				
			default:
				$fieldType = $_DB->getFieldType($_GET['table'], $_GET['field']);
				echo "<tr><td colspan=4>";
				switch($fieldType) {
					case "blob": 
						echo '<textarea id="'.$_GET['field'].'" name="'.$_GET['field'].'" class="'.$_GET['field'].'" style="width:380px; height:240px">'.$fieldValue[0][$_GET['field']].'</textarea><script>FF.contents.RTA(\''.$_GET['field'].'\', \'simple\');</script>';
						break;
					case "string":
						echo '<input type="text" name="'.$_GET['field'].'" value="'.$fieldValue[0][$_GET['field']].'" size="60" maxchars="255">';
						break;
					case "real":
						echo '<input type="text" name="'.$_GET['field'].'" value="'.$fieldValue[0][$_GET['field']].'">';
						break;
					case "int":
						echo '<input type="text" name="'.$_GET['field'].'" value="'.$fieldValue[0][$_GET['field']].'" size="5">';
						break;
					case "date":
						$date = $_DB->date_split($fieldValue[0][$_GET['field']]);
						?>
						<div id="samplecal" class="scal tinyscal"></div>
						<script>
						var options = ({
							month:<?=$date[1]?>,
							year:<?=$date[0]?>,
							day: <?=$date[2]?>
						});
						var samplecal = new scal('samplecal', '<?=$_GET['field']?>', options);
						</script>
						<?
						echo '<input type="hidden" id="'.$_GET['field'].'" name="'.$_GET['field'].'" value="'.$_DB->date_format($fieldValue[0][$_GET['field']]).'">';
						break;
					case "normal":
						$date = $_DB->date_split($fieldValue[0][$_GET['field']]);
						?>
						<div id="samplecal" class="scal tinyscal"></div>
						<script>
						var options = ({
							month:<?=$date[1]?>,
							year:<?=$date[0]?>,
							day: <?=$date[2]?>
						});
						var samplecal = new scal('samplecal', '<?=$_GET['field']?>', options);
						</script>
						<?
						echo '<input type="hidden" id="'.$_GET['field'].'" name="'.$_GET['field'].'" value="'.$_DB->date_format($fieldValue[0][$_GET['field']]).'">';
						break;
					default:
						echo '<input type="text" name="'.$_GET['field'].'" value="'.$fieldValue[0][$_GET['field']].'" size="60" maxchars="255">';
						break;
				}
				echo "</td></tr>";
				break;
		}
		?>
		</td></tr>
		</table>
		
		<div id="popupButtonRow">
			<input id="fastSaveButton" type="button" name="invia" value="SAVE" onClick="FF.contents.saveFastEdit(this, $('fastEditForm').serialize(), false);"> <input id="fastSaveButton" type="button" name="invia" value="SAVE & CLOSE" onClick="FF.contents.saveFastEdit(this, $('fastEditForm').serialize(), true);"> <input type="button" value="REFRESH" onClick="window.location = window.location.href">
		</div>
		
		</form>
		
	</div>
	
</body>
</html>
